package com.dlc.shop.search.common.listener;


import com.dlc.shop.bean.event.EsSearchProdEvent;
import com.dlc.shop.bean.vo.search.EsProductSearchVO;
import com.dlc.shop.common.util.EsPageVO;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.search.common.param.EsPageParam;
import com.dlc.shop.search.common.service.SearchProductService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 查询es商品列表
 * @author yami
 */
@Component("EsSearchProdListener")
@AllArgsConstructor
public class EsSearchProdListener {

    private final SearchProductService searchProductService;

    @EventListener(EsSearchProdEvent.class)
    public void defaultEsProductListener(EsSearchProdEvent event) {
        EsPageParam esPageParam = new EsPageParam();
        PageParam pageParam = event.getPageParam();
        esPageParam.setCurrent((int)pageParam.getCurrent());
        esPageParam.setSize((int)pageParam.getSize());
        EsPageVO<EsProductSearchVO> searchPage =  searchProductService.page(esPageParam, event.getProductParam(), Boolean.FALSE);
        PageParam<EsProductSearchVO> page = new PageParam<EsProductSearchVO>();
        page.setRecords(searchPage.getRecords());
        page.setTotal(searchPage.getTotal());
        page.setPages(searchPage.getPages());
        page.setSize(pageParam.getSize());
        event.setProductSearchPage(page);
    }
}
